// pages/personalInfo/personalInfo.js
import {
  userCenter,
  phone,
  bindTelAndPossWord
} from '../../utils/request/apiurl'
import api from '../../utils/request/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData: {},
    phone: '',
	uid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取个人信息
    api.post(userCenter.userData, {
      uid:this.data.uid|| wx.getStorageSync('userInfo').uid
    }).then(res => {
      // console.log(res)
      let a = res
      this.setData({
        phone: a.phone
      })
      if (a.phone) {
        a.phone = a.phone.substr(0, 3) + '****' + a.phone.substr(a.phone.length - 4)
      }
      this.setData({
        userData: a
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },

// 绑定密码
changePassWord(){
	if(this.data.phone!=''){
		wx.redirectTo({
			url: '../change-payPassWord/change-payPassWord?isPayPassword='+this.data.userData.isPayPassword+'&phone='+this.data.phone
		})
	}else{
		wx.showToast({
			title: "您还未绑定手机号码哦，请去绑定",
			icon: "none",
			duration: 2000
		});
	}	
},
//综合绑定
  comprehensivBind(){
    wx.redirectTo({
      url: '../comprehensivBind/comprehensivBind?isPayPassword=' + this.data.userData.isPayPassword + '&phone=' + this.data.phone
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
	var userInfo=wx.getStorageSync('userInfo')
	this.setData({
		uid:userInfo.uid
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
  	this.onLoad()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //上传图片
  changeImage() {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        let userData = _this.data.userData
        userData.headimgurl = tempFilePaths
        _this.setData({
          userData: userData
        })
      }
    })
  }
})